package com.aurora.source.source.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.aurora.source.R;
import com.aurora.source.base.BaseWhiteToolbarActivity;
import com.aurora.source.entity.UserEntity;
import com.aurora.source.source.presenter.ChangePasswordPresenter;
import com.aurora.source.source.view.IChangePasswordView;
import com.aurora.source.util.GreenDaoHelper;
import com.aurora.source.util.Validator;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class ChangePasswordActivity extends BaseWhiteToolbarActivity<ChangePasswordPresenter> implements IChangePasswordView {

    @BindView(R.id.et_password)
    EditText etPassword;
    @BindView(R.id.et_newpassword)
    EditText etNewpassword;
    @BindView(R.id.tv_register)
    TextView tvRegister;
    private String phone;
    private String messageCode;

    @Override
    public ChangePasswordPresenter attach() {
        return new ChangePasswordPresenter(this);
    }

    @Override
    public int bindLayout() {
        return R.layout.activity_change_password;
    }

    @Override
    public void initData(@Nullable Bundle bundle) {
        phone = bundle.getString("phone");
        messageCode = bundle.getString("messageCode");
    }

    @Override
    public void initView(Bundle savedInstanceState, View contentView) {
        simpleToolbar.setMainTitle("修改密码-短信验证");
        simpleToolbar.setLeftVisibility(View.VISIBLE);
    }

    @Override
    public void doBusiness() {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ButterKnife.bind(this);
    }

    @OnClick({R.id.tv_register})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_register:
                if(checkNull()){
                    return;
                }
                UserEntity userEntity = GreenDaoHelper.getDaoSession().getUserEntityDao().loadAll().get(0);

                Map<String, String> params = new HashMap<>();
                params.put("userAccount",userEntity.getUserAccount());
                params.put("messageCode",messageCode);
                params.put("userMobile",phone);
                params.put("userNewPwd",etPassword.getText().toString());
                basePresenter.changePassword(activity, params);
                break;



        }
    }

    private boolean checkNull() {
        if(TextUtils.isEmpty(etNewpassword.getText().toString())||TextUtils.isEmpty(etPassword.getText().toString())){
            showToast("请输入密码");
            return true;
        }

        if(!etNewpassword.getText().toString().equals(etPassword.getText().toString())){
            showToast("两次密码不一致");
            return true;
        }

        if(!Validator.isPasswordStrong(etNewpassword.getText().toString())){
            showToast("密码强度不够");
            return true;
        }
        return false;
    }

    @Override
    public void onChangePasswordSuccess(String msg) {
        showToast(msg);
        hideSoftInput();
        finish();
    }

    @Override
    public void onChangePasswordFail(String msg) {
        showToast(msg);
    }
}
